<?php

namespace app\http\middleware;

use think\Request;
use traits\controller\Jump;
use think\Facade\Session;
use app\common\server\Code;

class CheckLogin
{
    use Jump;

    //登录验证
    public function handle(Request $request, \Closure $next)
    {
        if(!$this->filterAction()){
            if ( !Session::get("admin_id") || !Session::get('admin_info') ) {
                if($request->isAjax()){
                    return apiReturn(Code::$error,"请登录后在操作!!!");
                }else{
                    $this->error("请登录后在操作!!!",url(config('admin.login_url')));
                }
            }
        }
        return $next($request);
    }

    /**
     * @param string $auth 过滤场景
     * @return bool
     */
    protected function filterAction(){
        $controller = request()->controller();
        $action= request()->action();
        $request_action = strtolower($controller .'/'. $action);
        return in_array($request_action,[
            'login/login',
            'login/logout'
        ]);
    }

}
